package androidx.preference;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import androidx.preference.Preference;
import b.d.g;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class PreferenceGroup extends Preference {
    final g<String, Long> Q;
    private final Handler R;
    private List<Preference> S;
    private boolean T;
    private int U;
    private boolean V;
    private int W;
    private b X;
    private final Runnable Y;

    class a implements Runnable {
        a() {
        }

        public void run() {
            synchronized (this) {
                PreferenceGroup.this.Q.clear();
            }
        }
    }

    public interface b {
        void a();
    }

    public interface c {
        int c(String str);

        int d(Preference preference);
    }

    static class d extends Preference.b {
        public static final Parcelable.Creator<d> CREATOR = new a();

        /* renamed from: b  reason: collision with root package name */
        int f788b;

        static class a implements Parcelable.Creator<d> {
            a() {
            }

            /* renamed from: a */
            public d createFromParcel(Parcel parcel) {
                return new d(parcel);
            }

            /* renamed from: b */
            public d[] newArray(int i) {
                return new d[i];
            }
        }

        d(Parcel parcel) {
            super(parcel);
            this.f788b = parcel.readInt();
        }

        d(Parcelable parcelable, int i) {
            super(parcelable);
            this.f788b = i;
        }

        public void writeToParcel(Parcel parcel, int i) {
            super.writeToParcel(parcel, i);
            parcel.writeInt(this.f788b);
        }
    }

    public PreferenceGroup(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public PreferenceGroup(Context context, AttributeSet attributeSet, int i) {
        this(context, attributeSet, i, 0);
    }

    public PreferenceGroup(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, attributeSet, i, i2);
        this.Q = new g<>();
        this.R = new Handler();
        this.T = true;
        this.U = 0;
        this.V = false;
        this.W = Integer.MAX_VALUE;
        this.X = null;
        this.Y = new a();
        this.S = new ArrayList();
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, t.PreferenceGroup, i, i2);
        int i3 = t.PreferenceGroup_orderingFromXml;
        this.T = b.f.d.c.g.b(obtainStyledAttributes, i3, i3, true);
        if (obtainStyledAttributes.hasValue(t.PreferenceGroup_initialExpandedChildrenCount)) {
            int i4 = t.PreferenceGroup_initialExpandedChildrenCount;
            Q0(b.f.d.c.g.d(obtainStyledAttributes, i4, i4, Integer.MAX_VALUE));
        }
        obtainStyledAttributes.recycle();
    }

    private boolean P0(Preference preference) {
        boolean remove;
        synchronized (this) {
            preference.W();
            if (preference.r() == this) {
                preference.a((PreferenceGroup) null);
            }
            remove = this.S.remove(preference);
            if (remove) {
                String o = preference.o();
                if (o != null) {
                    this.Q.put(o, Long.valueOf(preference.m()));
                    this.R.removeCallbacks(this.Y);
                    this.R.post(this.Y);
                }
                if (this.V) {
                    preference.S();
                }
            }
        }
        return remove;
    }

    public void F0(Preference preference) {
        G0(preference);
    }

    public boolean G0(Preference preference) {
        long j;
        if (this.S.contains(preference)) {
            return true;
        }
        if (preference.o() != null) {
            PreferenceGroup preferenceGroup = this;
            while (preferenceGroup.r() != null) {
                preferenceGroup = preferenceGroup.r();
            }
            String o = preference.o();
            if (preferenceGroup.H0(o) != null) {
                Log.e("PreferenceGroup", "Found duplicated key: \"" + o + "\". This can cause unintended behaviour, please use unique keys for every preference.");
            }
        }
        if (preference.q() == Integer.MAX_VALUE) {
            if (this.T) {
                int i = this.U;
                this.U = i + 1;
                preference.t0(i);
            }
            if (preference instanceof PreferenceGroup) {
                ((PreferenceGroup) preference).R0(this.T);
            }
        }
        int binarySearch = Collections.binarySearch(this.S, preference);
        if (binarySearch < 0) {
            binarySearch = (binarySearch * -1) - 1;
        }
        if (!N0(preference)) {
            return false;
        }
        synchronized (this) {
            this.S.add(binarySearch, preference);
        }
        j x = x();
        String o2 = preference.o();
        if (o2 == null || !this.Q.containsKey(o2)) {
            j = x.d();
        } else {
            j = this.Q.get(o2).longValue();
            this.Q.remove(o2);
        }
        preference.O(x, j);
        preference.a(this);
        if (this.V) {
            preference.M();
        }
        L();
        return true;
    }

    public <T extends Preference> T H0(CharSequence charSequence) {
        T H0;
        if (charSequence == null) {
            throw new IllegalArgumentException("Key cannot be null");
        } else if (TextUtils.equals(o(), charSequence)) {
            return this;
        } else {
            int L0 = L0();
            for (int i = 0; i < L0; i++) {
                T K0 = K0(i);
                if (TextUtils.equals(K0.o(), charSequence)) {
                    return K0;
                }
                if ((K0 instanceof PreferenceGroup) && (H0 = ((PreferenceGroup) K0).H0(charSequence)) != null) {
                    return H0;
                }
            }
            return null;
        }
    }

    public int I0() {
        return this.W;
    }

    public b J0() {
        return this.X;
    }

    public void K(boolean z) {
        super.K(z);
        int L0 = L0();
        for (int i = 0; i < L0; i++) {
            K0(i).V(this, z);
        }
    }

    public Preference K0(int i) {
        return this.S.get(i);
    }

    public int L0() {
        return this.S.size();
    }

    public void M() {
        super.M();
        this.V = true;
        int L0 = L0();
        for (int i = 0; i < L0; i++) {
            K0(i).M();
        }
    }

    /* access modifiers changed from: protected */
    public boolean M0() {
        return true;
    }

    /* access modifiers changed from: protected */
    public boolean N0(Preference preference) {
        preference.V(this, A0());
        return true;
    }

    public boolean O0(Preference preference) {
        boolean P0 = P0(preference);
        L();
        return P0;
    }

    public void Q0(int i) {
        if (i != Integer.MAX_VALUE && !D()) {
            Log.e("PreferenceGroup", getClass().getSimpleName() + " should have a key defined if it contains an expandable preference");
        }
        this.W = i;
    }

    public void R0(boolean z) {
        this.T = z;
    }

    public void S() {
        super.S();
        this.V = false;
        int L0 = L0();
        for (int i = 0; i < L0; i++) {
            K0(i).S();
        }
    }

    /* access modifiers changed from: package-private */
    public void S0() {
        synchronized (this) {
            Collections.sort(this.S);
        }
    }

    /* access modifiers changed from: protected */
    public void X(Parcelable parcelable) {
        if (parcelable == null || !parcelable.getClass().equals(d.class)) {
            super.X(parcelable);
            return;
        }
        d dVar = (d) parcelable;
        this.W = dVar.f788b;
        super.X(dVar.getSuperState());
    }

    /* access modifiers changed from: protected */
    public Parcelable Y() {
        return new d(super.Y(), this.W);
    }

    /* access modifiers changed from: protected */
    public void e(Bundle bundle) {
        super.e(bundle);
        int L0 = L0();
        for (int i = 0; i < L0; i++) {
            K0(i).e(bundle);
        }
    }

    /* access modifiers changed from: protected */
    public void f(Bundle bundle) {
        super.f(bundle);
        int L0 = L0();
        for (int i = 0; i < L0; i++) {
            K0(i).f(bundle);
        }
    }
}
